<?php
namespace jy_newpingfee;
defined('BY_JYA') or exit('error');
/*
	任务解锁
*/
class renwu extends \plugin{
	/*
		downs
	*/
	public function canyu($params,$pinggoods=array()){
		$fromuser=pdo_getcolumn('jy_newpingfee_member',array('mid'=>$params['mid'],'uniacid'=>$_SESSION['uniacid']),'fromuser');
		$isnew=pdo_count('jy_newpingfee_log',array('status'=>1,'type'=>0,'mid' =>$params['mid']),'id');
		//第一次参与，有释放虚拟币机会！
		if($isnew==1){
			$this->up_daycanyus($fromuser);
		}
		$basic=$this->plugin_setting('basic');
		if($basic['task_open']!=1){
			return false;
		}
		$this->commission_order=array(
			'uniacid'=>$_SESSION['uniacid'],
			'mid'=>$params['mid'],
			'orderid'=>$params['orderid'],
			'order_price'=>$pinggoods['price'],
			'goodscommissions'=>json_decode($pinggoods['commissions'],true),
			'commission_money'=>0,
			'team_money'=>0,
			'red_money'=>0,
			'fen_futype'=>$basic['fen_futype']?:1,
		);
		for($i=0;$i<3;$i++){
				if(!$fromuser){
					break;
				}
				$member=$this->get('member',array('mid'=>$fromuser,'uniacid'=>$_SESSION['uniacid']));
				$level=$this->get('level',array('id'=>$member['level_id']));
				$this->create_commission($member,$level,$i);
				$level['level']=$level['level']?:0;
				$fromuser=$member['fromuser'];
		}
		//自购升级
		$this->model('member')->update_agent_level($params['mid'],true,$pinggoods);
		//上级升级
		if($fromuser){
			$this->model('member')->update_agent_level($fromuser);
			//上上级升级
			$shfromuser=pdo_getcolumn('jy_newpingfee_member',array('mid'=>$fromuser,'uniacid'=>$_SESSION['uniacid']),'fromuser');
			if($shfromuser){
				$this->model('member')->update_agent_level($shfromuser);
			}
		}
		return false;
	}
	//拼团成功奖励
	public function canfen($params,$pinggoods=array()){
		$basic=$this->plugin_setting('basic');
		if($basic['task_open']!=1){
			return false;
		}
		$this->pinggoods=$pinggoods;
		$this->commission_order=array(
			'uniacid'=>$_SESSION['uniacid'],
			'mid'=>$params['mid'],
			'orderid'=>$params['id'],
			'order_price'=>$pinggoods['price'],
			'goodscommissions'=>json_decode($pinggoods['commissions'],true),
			'commission_money'=>0,
			'team_money'=>0,
			'red_money'=>0,
			'fen_futype'=>$basic['fen_futype']?:1,
		);
		$member=pdo_get('jy_newpingfee_member',array('mid'=>$params['mid'],'uniacid'=>$_SESSION['uniacid']));
		$fromuser=$member['fromuser'];
		if($member['level_id']){
			$lastlevel=$this->getcolumn('level',array('id'=>$member['level_id']),'level');
		}else{
			$lastlevel=0;
		}
		$parameter=array(
			'params'=>array('uniacid'=>$_SESSION['uniacid']),
			'orderby'=>'level asc',
			'limit'=>'1',
		);		
		$levels=$this->get_list('level',$parameter);
		$minlevel=$levels[0]['level'];
		$parameter['orderby']='level desc';
		$levels=$this->get_list('level',$parameter);
		$this->commission_order['maxlevel']=$maxlevel=$levels[0]['level'];
		$member=$this->get('member',array('mid'=>$fromuser,'uniacid'=>$_SESSION['uniacid']));
		$level=$this->get('level',array('id'=>$member['level_id']));
		$this->create_teamcommission($member,$level,0);
		return false;
	}
	/*
		更新状态;
	*/
	function order_complete($orderid){
		$this->update('commission',array('status'=>1,'complete_time'=>TIMESTAMP),array('orderid'=>$orderid));
		return true;
	}
	/*
		直推、间推;
	*/
	function create_commission($agent,$level=array(),$i=0){
			$commission_level=$agent['level_id'];
			if(!$agent['level_id']){
				$commissions=$this->plugin_setting('default_level')['commissions'];
				$commission_per=$commissions['commission_'.$i];
			}else{
				$level_id=$level['id'];
				$commissions=json_decode($level['commissions'],true);
				$commission_per=$commissions['commission_'.$i];
			}
			$money=0;
			$order_price=$this->commission_order['order_price'];		
			//先检查是否存在商品独立分红规则，如果存在则对订单相应商品部分金额单独计算分红
			$goods_commission=$this->commission_order['goodscommissions'];
			foreach($goods_commission as $gv){
				if($agent['level_id']==$gv['level_id']){
					$goods_commission_per=$gv['commissions']['commission_'.$i];
					$isgoodsper=1;
					if(substr($goods_commission_per,-1)=='%'){
							$money=intval($order_price*substr($goods_commission_per,0,-1))/100;
					}else{
							$money=$goods_commission_per;
					}
					break;
				}
			}	
			if(!$isgoodsper){
				if(substr($commission_per,-1)=='%'){
					$money=intval($order_price*substr($commission_per,0,-1))/100;
				}else{
					$money=$commission_per;
				}
			}
			if($money>0){
				$new_commission=array(
					'uniacid'=>$_SESSION['uniacid'],
					'mid'=>$agent['mid'],
					'orderid'=>$this->commission_order['orderid'],
					'money'=>$money,
					'createtime'=>TIMESTAMP,
					'status'=>0,
					'type'=>5,
					'level'=>$i,
					'fen_futype'=>$this->commission_order['fen_futype'],
				);
				$this->insert('commission',$new_commission);
				if($i==0){
					//拿贡献奖
					$this->create_shangjifee($new_commission,$agent['mid']);
					//拿感恩奖(上级+上上级)
					$this->create_xiajifee($new_commission,$agent['fromuser']);
				}
			}
			return true;
	}
	/*
		拼团成功奖励;
	*/
	function create_teamcommission($agent,$level=array(),$i=0){
			$commission_level=$agent['level_id'];
			if(!$agent['level_id']){
				$commissions=$this->plugin_setting('default_level')['commissions'];
				$commission_per=$commissions['teamfee'];
			}else{
				$level_id=$level['id'];
				$commissions=json_decode($level['commissions'],true);
				$commission_per=$commissions['teamfee'];
			}
			$money=0;
			$order_price=$this->commission_order['order_price'];		
			//先检查是否存在商品独立分红规则，如果存在则对订单相应商品部分金额单独计算分红
			$goods_commission=$this->commission_order['goodscommissions'];
			foreach($goods_commission as $gv){
				if($agent['level_id']==$gv['level_id']){
					$goods_commission_per=$gv['commissions']['teamfee'];
					$isgoodsper=1;
					if(substr($goods_commission_per,-1)=='%'){
							$money=intval($order_price*substr($goods_commission_per,0,-1))/100;
					}else{
							$money=$goods_commission_per;
					}
					break;
				}
			}	
			if(!$isgoodsper){
				if(substr($commission_per,-1)=='%'){
					$money=intval($order_price*substr($commission_per,0,-1))/100;
				}else{
					$money=$commission_per;
				}
			}
			if($money>0){
				$new_commission=array(
					'uniacid'=>$_SESSION['uniacid'],
					'mid'=>$agent['mid'],
					'orderid'=>$this->commission_order['orderid'],
					'money'=>$money,
					'createtime'=>TIMESTAMP,
					'status'=>1,
					'type'=>0,
					'level'=>$i,
					'fen_futype'=>$this->commission_order['fen_futype'],
				);
				$this->insert('commission',$new_commission);
				if($i==0){
					//拿贡献奖
					//$this->create_shangjifee($new_commission,$agent['mid'],1);
					//拿感恩奖
					//$this->create_xiajifee($new_commission,$agent['fromuser'],1);
				}
			}
			return true;
	}
	/*
		拿感恩奖,拿下级收入;
	*/
	function create_xiajifee($new_commission,$fromuser=0,$status=0){
		for($i=0;$i<3;$i++){
			if(!$fromuser){
				break;
			}
			$agent=pdo_get('jy_newpingfee_member',array('mid'=>$fromuser,'uniacid'=>$_SESSION['uniacid']));
			$fromuser=$agent['fromuser'];
			if(!$agent['level_id']){
				$commissions=$this->plugin_setting('default_level')['commissions'];
				$commission_per=$commissions['xiaji_'.$i];
			}else{
				$level=pdo_get('jy_newpingfee_level',array('id'=>$agent['level_id'],'uniacid'=>$_SESSION['uniacid']));
				$level_id=$level['id'];
				$commissions=json_decode($level['commissions'],true);
				$commission_per=$commissions['xiaji_'.$i];
			}
			
			$money=0;
			$order_price=$new_commission['money'];		
			//先检查是否存在商品独立分红规则，如果存在则对订单相应商品部分金额单独计算分红
			$goods_commission=$this->commission_order['goodscommissions'];
			foreach($goods_commission as $gv){
				if($agent['level_id']==$gv['level_id']){
					$goods_commission_per=$gv['commissions']['xiaji_'.$i];
					$isgoodsper=1;
					if(substr($goods_commission_per,-1)=='%'){
							$money=intval($order_price*substr($goods_commission_per,0,-1))/100;
					}else{
							$money=$goods_commission_per;
					}
					break;
				}
			}	
			if(!$isgoodsper){
				if(substr($commission_per,-1)=='%'){
					$money=intval($order_price*substr($commission_per,0,-1))/100;
				}else{
					$money=$commission_per;
				}
			}
			if($money>0){
				$new_commission=array(
					'uniacid'=>$_SESSION['uniacid'],
					'mid'=>$agent['mid'],
					'orderid'=>$new_commission['orderid'],
					'money'=>$money,
					'createtime'=>TIMESTAMP,
					'status'=>$status,
					'type'=>1,
					'level'=>$i,
					'fen_futype'=>$new_commission['fen_futype'],
				);
				$this->insert('commission',$new_commission);
			}
		}
		return true;
	}
	/*
		拿贡献奖,下级获得收入;
	*/
	function create_shangjifee($new_commission,$fromuser=0,$status=0){
		for($i=0;$i<3;$i++){
			if(!$fromuser){
				break;
			}
			$agent=pdo_get('jy_newpingfee_member',array('fromuser'=>$fromuser,'uniacid'=>$_SESSION['uniacid']));
			$fromuser=$agent['mid'];
			if(!$agent['level_id']){
				$commissions=$this->plugin_setting('default_level')['commissions'];
				$commission_per=$commissions['shangji_'.$i];
			}else{
				$level=pdo_get('jy_newpingfee_level',array('id'=>$agent['level_id'],'uniacid'=>$_SESSION['uniacid']));
				$level_id=$level['id'];
				$commissions=json_decode($level['commissions'],true);
				$commission_per=$commissions['shangji_'.$i];
			}
			
			$money=0;
			$order_price=$new_commission['money'];		
			//先检查是否存在商品独立分红规则，如果存在则对订单相应商品部分金额单独计算分红
			$goods_commission=$this->commission_order['goodscommissions'];
			foreach($goods_commission as $gv){
				if($agent['level_id']==$gv['level_id']){
					$goods_commission_per=$gv['commissions']['shangji_'.$i];
					$isgoodsper=1;
					if(substr($goods_commission_per,-1)=='%'){
							$money=intval($order_price*substr($goods_commission_per,0,-1))/100;
					}else{
							$money=$goods_commission_per;
					}
					break;
				}
			}	
			if(!$isgoodsper){
				if(substr($commission_per,-1)=='%'){
					$money=intval($order_price*substr($commission_per,0,-1))/100;
				}else{
					$money=$commission_per;
				}
			}
			if($money>0){
				$new_commission=array(
					'uniacid'=>$_SESSION['uniacid'],
					'mid'=>$agent['mid'],
					'orderid'=>$new_commission['orderid'],
					'money'=>$money,
					'createtime'=>TIMESTAMP,
					'status'=>$status,
					'type'=>2,
					'level'=>$i,
					'fen_futype'=>$new_commission['fen_futype'],
				);
				$this->insert('commission',$new_commission);
			}
		}
		return true;
	}
	/*
		更新进入新参与;
	*/
	function up_daycanyus($mid){
		$this->model('member')->up_tixianfee($mid,true);
		$dayid=pdo_getcolumn('jy_newpingfee_member',array('mid'=>$mid),'dayid');
		$newdayid=date('Y-m-d',TIMESTAMP);
		if($dayid==$newdayid){
			pdo_update('jy_newpingfee_member',array('daycanyus +='=>1),array('mid'=>$mid));
		}else{
			pdo_update('jy_newpingfee_member',array('daycanyus'=>1,'dayid'=>$newdayid),array('mid'=>$mid));
		}
		return true;
	}
}